//
//  NWStatusOriginalView.swift
//  Weibo
//
//  Created by Northwan on 16/1/24.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit
import SnapKit
//原创微博
class NWStatusOriginalView: UIView {
    
    private var bottomConstraint:Constraint?
    
    
    var statusViewModel:NWStatusViewModel?{
        didSet{
            let urlString = NSURL(string: statusViewModel!.headImageStr ?? "")
            avatarImageView.sd_setImageWithURL(urlString, placeholderImage: UIImage(named: "avatar_default_big"))
            
            nameLabel.text = statusViewModel?.status?.user?.name
            
            vipImageView.image = statusViewModel?.vipImage
            
            timeLabel.text  = statusViewModel?.createAtText
            
            sourceLabel.text = statusViewModel?.sourceText
            
            verifiedImageView.image = statusViewModel?.verifiedImage
            
            contentLabel.text = statusViewModel?.status?.text
            
            bottomConstraint?.uninstall()
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0{
                photosView.hidden = false
                photosView.pic_urls = pic_urls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(photosView.snp_bottom).offset(NWStatusCellSubViewsMargin).constraint
                })
            }else{
                photosView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(NWStatusCellSubViewsMargin).constraint
                })
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addSubview(avatarImageView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(photosView)
        
        avatarImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(NWStatusCellSubViewsMargin)
            make.top.equalTo(self).offset(NWStatusCellSubViewsMargin)
            make.width.equalTo(35)
            make.height.equalTo(35)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(avatarImageView)
            make.leading.equalTo(avatarImageView.snp_trailing).offset(NWStatusCellSubViewsMargin)
        }
        
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel.snp_centerY)
            make.leading.equalTo(nameLabel.snp_trailing).offset(NWStatusCellSubViewsMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(avatarImageView.snp_bottom)
            make.leading.equalTo(avatarImageView.snp_trailing).offset(NWStatusCellSubViewsMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(NWStatusCellSubViewsMargin)
        }
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(avatarImageView.snp_bottom)
            make.centerX.equalTo(avatarImageView.snp_trailing)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(avatarImageView.snp_bottom).offset(NWStatusCellSubViewsMargin)
            make.leading.equalTo(avatarImageView.snp_leading)
        }
        
        photosView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(NWStatusCellSubViewsMargin)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(NWStatusCellSubViewsMargin).constraint
        }
    }
    
    // MARK: - 懒加载
    private lazy var avatarImageView:UIImageView = {
       let avatarImageView =  UIImageView(image: UIImage(named: "avatar_default_big"))
        return avatarImageView
    }()
    private lazy var nameLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    
    private lazy var vipImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    private lazy var timeLabel:UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 10)
    
    private lazy var sourceLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 10)
    
    private lazy var verifiedImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
    
    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15, maxWidth: SCREENW - 2 * NWStatusCellSubViewsMargin)
    
    private lazy var photosView:NWStatusPhotosView = {
        let view = NWStatusPhotosView()
        view.backgroundColor = self.backgroundColor
        return view
    }()

}
